package com.zyh.method;

public class MethodDemo3 {
    public static void main(String[] args) {
        /*
        求出数组的最大值最小值元素和平均值,存入新的数组
         */
        int[] arr = {5, 12, 90, 18, 77, 76, 45, 28, 59, 72};
        if (arr==null){
            System.err.println("数据有误!");
        }
        double[] doubles = newArray(arr);
        for (int i = 0; i < doubles.length; i++) {
            System.out.print(doubles[i]+" ");
        }
    }

    public static double[] newArray(int arr[]) {
//        判断数组是否合法
        if (arr == null || arr.length == 0){
            return null;
        }
        int max=arr[0],min=arr[0];
        double sum=0.0,avg=0.0;
        for (int i = 0; i < arr.length; i++) {
//            求最大值
            if (arr[i]>max){
                max=arr[i];
            }
//            求最小值
            if (arr[i]<min){
                min=arr[i];
            }
//            求和
            sum=sum+arr[i];
        }
        avg=sum/arr.length;
        double newArr[]=new double[4];
        for (int i = 0; i < newArr.length; i++) {
            newArr[0]=max;
            newArr[1]=min;
            newArr[2]=sum;
            newArr[3]=avg;
        }
        return newArr;
    }
}
